package com.Txunda.fxw.ui.fgt;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.Txunda.fxw.R;
import com.Txunda.fxw.base.utils.tool.Toolkit;
import com.Txunda.fxw.base.utils.util.JSONUtils;
import com.Txunda.fxw.base.utils.util.L;
import com.Txunda.fxw.base.utils.util.ListUtils;
import com.Txunda.fxw.base.utils.util.MapUtils;
import com.Txunda.fxw.ui.activity.carve.CarveDetailsAty;
import com.Txunda.fxw.ui.activity.carve.CarveListAty;
import com.Txunda.fxw.ui.adapter.model.LawModel;
import com.Txunda.fxw.ui.adapter.model.SummaryModel;
import com.Txunda.fxw.ui.base.BaseFgt;
import com.Txunda.fxw.ui.http.Syb;
import com.Txunda.fxw.ui.toolkit.glide.GlideUtils;
import com.Txunda.fxw.ui.toolkit.util.DensityUtil;
import com.Txunda.fxw.ui.toolkit.util.PreferenceUtil;
import com.Txunda.fxw.ui.widget.XCSlideView;
import com.Txunda.fxw.view.RoundedImageView;
import com.bumptech.glide.Glide;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Business2Fgt extends BaseFgt {

    private List<SummaryModel> list_data;
    @ViewInject(R.id.aty_title3_tv_center)
    private TextView aty_title3_tv_center;
    @ViewInject(R.id.aty_title3_icon)
    private ImageView aty_title3_icon;
    @ViewInject(R.id.aty_title3_tv_right2)
    private TextView aty_title3_tv_right2;
    @ViewInject(R.id.icon_logo)
    private RoundedImageView icon_logo;
    private String trl;
    @ViewInject(R.id.carve_list_lv)
    private ListView carve_list_lv;

    private XCSlideView mSlideViewRight;
    private ListView area_lv;
    private MyAdapter adapter;

    private Bundle bundle;
    private String tea_id = "1";

    private TextView tv_count; // 全部商圈总数
    private String district_id;
    private String city_id = "";
    private ArrayList<Map<String, String>> company_list_data;
    private CompanyAdapter companyAdapter;
    private ArrayList<SummaryModel> cityList;
    private ArrayList<SummaryModel> districtList;

    protected int index;

    private Syb syb;

    private String district_name;

    @Override
    protected int getLayoutResId() {
        return R.layout.fgt_business2;
    }

    @Override
    protected void initialized() {
        districtList = new ArrayList<>();
        cityList = new ArrayList<>();

        companyAdapter = new CompanyAdapter();
        company_list_data = new ArrayList<>();

        list_data = new ArrayList<>();
    }

    @Override
    protected void requestData() {
        aty_title3_icon.setVisibility(View.GONE);
        aty_title3_tv_center.setText("汽车租赁平台");
        String logo = PreferenceUtil.readString(getContext(), PreferenceUtil.sp_logo);
        int size = Toolkit.dip2px(getContext(), 50);
        GlideUtils.urlCenterCrop(this, logo, size, size, icon_logo);
        aty_title3_tv_right2.setVisibility(View.VISIBLE);
        String area_name = PreferenceUtil.readString(getContext(), PreferenceUtil.sp_mode);
        if (district_name == null || district_name.equals("")) {
            aty_title3_tv_right2.setText(area_name);
        } else {
            aty_title3_tv_right2.setText(district_name);
        }
        // 侧滑
        Context mContext = getActivity();
        // 屏幕宽度
        int mScreenWidth = DensityUtil.getScreenWidthAndHeight(mContext)[0];
        mSlideViewRight = XCSlideView.create(getContext(), XCSlideView.Positon.RIGHT);
        View menuViewRight = LayoutInflater.from(mContext).inflate(R.layout.layout_area, null);
        mSlideViewRight.setMenuView(getContext(), menuViewRight);
        tv_count = menuViewRight.findViewById(R.id.tv_count);
        mSlideViewRight.setMenuWidth(mScreenWidth * 6 / 9);

        area_lv = menuViewRight.findViewById(R.id.area_list);
        syb = new Syb();

        carve_list_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                bundle = new Bundle();
//                bundle.putString("tea_id", tea_id);
//                String syb_id = company_list_data.get(position).get("syb_id");
//                String name = company_list_data.get(position).get("name");
//                bundle.putString("syb_id", syb_id);
//                bundle.putString("name", name);
//                bundle.putString("district_name", district_name);
//                startActivity(CarveDetailsAty.class, bundle);
                Map<String, String> item = company_list_data.get(position);
                trl = item.get("syb_id");
                syb.sybInfo(item.get("syb_id"), Business2Fgt.this);
//
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        initHttp();
    }

    private void initHttp() {
        syb.servicePlatform(city_id, tea_id, this);
    }

    /**
     * 商家列表adapter
     *
     * @author Administrator
     */
    public class CompanyAdapter extends BaseAdapter {

        CompanyAdapter.MyViewHolder vh;

        @Override
        public int getCount() {
            int ret = 0;
            if (company_list_data != null) {
                ret = company_list_data.size();
            }
            return ret;
        }

        @Override
        public Object getItem(int position) {
            return company_list_data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_carve_list, parent, false);
                vh = new CompanyAdapter.MyViewHolder();
                ViewUtils.inject(vh, convertView);
                convertView.setTag(vh);
            } else {
                vh = (CompanyAdapter.MyViewHolder) convertView.getTag();
            }
            Glide.with(getContext())
                    .load(company_list_data.get(position).get("cover_pic"))
                    .into(vh.item_carve_img);
            vh.item_carve_title.setText(company_list_data.get(position).get("name"));

            return convertView;
        }

        class MyViewHolder {
            @ViewInject(R.id.item_carve_img)
            private ImageView item_carve_img;
            @ViewInject(R.id.item_carve_title)
            private TextView item_carve_title;

        }

    }

    /**
     * 地区筛选adapter
     *
     * @author Administrator
     */
    public class MyAdapter extends BaseAdapter {
        List<SummaryModel> list;
        MyAdapter.MyViewHolder vh;

        public MyAdapter(List<SummaryModel> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            int ret = 0;
            if (list != null) {
                ret = list.size();
            }
            return ret;
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_area_lv, parent, false);
                vh = new MyAdapter.MyViewHolder();
                ViewUtils.inject(vh, convertView);
                convertView.setTag(vh);
            } else {
                vh = (MyAdapter.MyViewHolder) convertView.getTag();
            }
            vh.item_area_name.setText(list.get(position).getName()); // 地区名称
            vh.item_area_num.setText(list.get(position).getCount()); // 数量

            return convertView;
        }

        class MyViewHolder {
            @ViewInject(R.id.item_area_name)
            private TextView item_area_name;
            @ViewInject(R.id.item_area_num)
            private TextView item_area_num;
        }

    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
        if (MapUtils.isEmpty(map)) {
            return;
        }
        if (requestUrl.contains("Syb/servicePlatform")) { // 商家列表
            servicePlatform(map);
            return;
        }
        if (requestUrl.contains("summaryCity")) { // 市级
            summaryCity(map);
            return;
        }
        if (requestUrl.contains("summaryDistrict")) { // 地区
            summaryDistrict(map);
            return;
        }
        if (requestUrl.contains("summary")) { // 省级地区
            summary(map);
        }

        if (requestUrl.contains("Syb/sybInfo")) { // 商家详情
            Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
            if (MapUtils.isEmpty(data)) {
                return;
            }
            //电话
            //图文链接
            Bundle bundle = new Bundle();
            bundle.putString("type", "q");
            bundle.putString("class_id", trl);
            bundle.putString("com_id", trl);
            bundle.putString("count", "1");
            bundle.putString("tel", data.get("tel"));
            bundle.putString("url", data.get("url"));
            startActivity(WebAty.class, bundle);
        }

    }

    @Override
    public void onError(String requestUrl, Map<String, String> error) {
        removeProgressDialog();
        removeProgressContent();

        try {
            district_name = cityList.get(index).getName();
            // showToast("已切换至" + districtName);
            aty_title3_tv_right2.setText(district_name);
            adapter.notifyDataSetChanged();
            carve_list_lv.setVisibility(View.GONE);
            showToast("暂无数据");
            if (mSlideViewRight.isShow()) {
                mSlideViewRight.dismiss();
            }
        } catch (Exception e) {
            L.e(e.getMessage());
        }

    }

    private void servicePlatform(Map<String, String> map) {
        Log.d("zdl", "======================" + map.toString());
        carve_list_lv.setVisibility(View.VISIBLE);
        company_list_data.clear();
        company_list_data = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
        carve_list_lv.setAdapter(companyAdapter);
    }

    private void summaryCity(Map<String, String> map) {
        cityList.clear();
        Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
        if (MapUtils.isEmpty(data)) {
            return;
        }
        String count = data.get("count");
        tv_count.setText(count);
        List<Map<String, String>> tempList = JSONUtils.parseKeyAndValueToMapList(data.get("list"));
        if (ListUtils.isEmpty(tempList)) {
            return;
        }
        for (Map<String, String> temp : tempList) {
            cityList.add(new SummaryModel(temp.get("city_name"), temp.get("city_id"), temp.get("c_count")));
        }

        adapter = new MyAdapter(cityList);
        area_lv.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        area_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                city_id = cityList.get(position).getId(); // id
                aty_title3_tv_right2.setText(cityList.get(position).getName());
                index = position;
                PreferenceUtil.write((getContext()), PreferenceUtil.sp_mode, cityList.get(position).getName());
                adapter.notifyDataSetChanged();

                showProgressDialog();
                syb.summaryDistrict(tea_id, city_id, Business2Fgt.this); // 区县集合
            }
        });
    }

    private void summaryDistrict(Map<String, String> map) {
        if ("success".equals(map.get("flag"))) {
            districtList.clear();
            Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
            if (MapUtils.isEmpty(data)) {
                return;
            }
            List<Map<String, String>> tempList = JSONUtils.parseKeyAndValueToMapList(data.get("list"));
            if (ListUtils.isEmpty(tempList)) {
                return;
            }
            for (Map<String, String> temp : tempList) {
                districtList.add(new SummaryModel(temp.get("city_name"), temp.get("district_id"), temp.get("c_count")));
            }
            adapter = new MyAdapter(districtList);
            area_lv.setAdapter(adapter);
            adapter.notifyDataSetChanged();
            syb.servicePlatform(city_id, tea_id, this);
            area_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    // 地区id
                    district_name = districtList.get(position).getName();
                    showToast("已切换至" + district_name);
                    aty_title3_tv_right2.setText(district_name);
                    adapter.notifyDataSetChanged();

                    initHttp();
                    if (mSlideViewRight.isShow()) {
                        mSlideViewRight.dismiss();
                    }
                }
            });
        }
    }

    private void summary(Map<String, String> map) {
        if ("success".equals(map.get("flag"))) {
            list_data.clear();
            final Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
            if (MapUtils.isEmpty(data)) {
                return;
            }
            String count = data.get("count"); // 全部商圈总数
            tv_count.setText(count);
            List<Map<String, String>> tempData = JSONUtils.parseKeyAndValueToMapList(data.get("list"));
            if (ListUtils.isEmpty(tempData)) {
                return;
            }
            for (Map<String, String> temp : tempData) {
                list_data.add(new SummaryModel(temp.get("province_name"), temp.get("province_id"), temp.get("c_count")));
            }

            adapter = new MyAdapter(list_data);
            area_lv.setAdapter(adapter);

            area_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    district_id = list_data.get(position).getId(); // 地区id
                    showProgressDialog();
                    syb.summaryCity(tea_id, district_id, Business2Fgt.this); // 省集合
                    adapter.notifyDataSetChanged();
                }
            });
        }
    }

    //点击事件
    @Override
    @OnClick({R.id.icon_logo})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.icon_logo: // logo
                if (!mSlideViewRight.isShow()) {
                    mSlideViewRight.show();
                }
                showProgressDialog();
                syb.summary(tea_id, this);
                break;
            default:
                break;
        }
    }
}
